import contentHandler from "/handlers/contentHandler.js";
import todoHandler from "/handlers/todoHandler.js";
import { isBgBlack } from "/handlers/isBgBlack.js";
import { deleteHandler } from "/handlers/deleteHandler.js";
import { saveNote } from "/handlers/saveNote.js";
import { newHandler } from "/handlers/newHandler.js";
import { logout } from "/handlers/logout.js";
import { isNull } from "/handlers/isNull.js";
$(document).ready(function () {
    contentHandler.whenBlur();
    contentHandler.whenFocus();
    $('.color-option').click(function () {
        let nid = $('#nid-box')[0].innerHTML;
        let title = $('#title').val();
        let content = $('#content').val();
        let color = $(this).attr("id");
        color = color.replace("H", "#");
        isBgBlack(color);
        saveNote(nid, content, title, color);
    })
    $('.ninfo-box-newnote').click(function () {
        $('.right-container').find("*").css("display", "inline-block")
        let uid = $('.center-top-uid').eq(0).text();
        newHandler('note', uid);
    })
    $('.tinfo-box-newtodo').click(function () {
        let todotext = $('.tinfo-box-todotext').eq(0);
        todotext.val('');
        todotext.animate({ width: '460px' }, 300)
    })
    $('.tinfo-box-todotext').blur(function () {
        let todotext = $('.tinfo-box-todotext').eq(0);
        todotext.animate({ width: '0' })
        if (!isNull(todotext.val())) {
            let uid = $('.center-top-uid').eq(0).text();
            newHandler('todo', uid, todotext.val());
            todoHandler.flashUpDone();
        }
    })
    $('#delete').click(function () {
        let nid = $('#nid-box')[0].innerHTML;
        deleteHandler('note', nid);
    })
    $('.todo-container').on("click", ".delete-todo", function () {
        let tid = $(this).attr('id').replace("d-td", "");
        deleteHandler('todo', tid);
    })
    $('.logoutBtn').eq(0).click(function () {
        logout();
    })
    $('#bold').click(function () {
        if ($('#content').css("font-weight") == '400') {
            $('#content').css("font-weight", "bold");
        } else {
            $('#content').css("font-weight", "normal");
        }
    })
    $('#itali').click(function () {
        if ($('#content').css("font-style") == "normal") {
            $('#content').css("font-style", "italic");
        } else {
            $('#content').css("font-style", "normal");
        }
    })
    $('#underline').click(function () {
        if ($('#content').css("text-decoration") == "none solid rgb(0, 0, 0)") {
            $('#content').css("text-decoration", "underline");
        } else {
            $('#content').css("text-decoration", "none");
        }
    })
    Qmsg.config({
        showClose: false,
        timeout: 1500
    })
    let bid = $('.center-top-uid').eq(0).text();
    $('.noteli').click(function () {
        $('.todo-container').eq(0).css('display', 'none');
        $('.right-container').eq(0).css('display', 'inline-block');
        let elid = $(this).attr("id");
        let liid = elid.replace("li", "");
        axios.get(`/notes/getSelectedNote/${liid}`)
            .then(res => {
                console.log(res.data);
                let title = res.data[0].title;
                let content = res.data[0].content;
                let color = res.data[0].backgroundColor;
                isBgBlack(color);
                $('#title').val(title);
                $('#content').val(content);
                $('#nid-box')[0].innerHTML = liid;
            })
            .catch(error => {
                alert(error);
            })
    });
    let firstNid;
    if ($('.noteli').eq(0).attr("id")) {
        let firstLiid = $('.noteli').eq(0).attr("id");
        firstNid = firstLiid.replace("li", "");
        axios.get(`/notes/getSelectedNote/${firstNid}`)
            .then(res => {
                console.log(res.data);
                let color = res.data[0].backgroundColor;
                isBgBlack(color);
                $('#title').val(res.data[0].title);
                $('#content').val(res.data[0].content);
                $('#nid-box')[0].innerHTML = firstNid;
            })
            .catch(error => {
                alert(error);
            })
    } else {
        $('.right-container').find("*").css("display", "none");
    }
    $('.doneBtn').click(function () {
        todoHandler.flashDone();
    })
    $('.updoneBtn').click(function () {
        todoHandler.flashUpDone();
    })
    $('.todo-container').on("click", '.tdradio', async function () {
        let currentTid = $(this).attr('id').replace("td", "");
        let isslc = $(this).attr("data-isSelect");
        let sttrsl;
        if (isslc === "false") {
            sttrsl = await todoHandler.changeState(currentTid, 1);
        } else if (isslc === "true") {
            sttrsl = await todoHandler.changeState(currentTid, 0);
        }
        switch (sttrsl) {
            case 1:
                $(this).css("backgroundColor", "rgb(3, 79, 111)");
                $(this).attr("data-isSelect", "true");
                break;
            case 2:
                $(this).css("backgroundColor", "transparent");
                $(this).attr("data-isSelect", "false");
                break;
            case 0:
                alert("出错了")
            default:
                break;
        }
    })
});